package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Branch;
import com.three.entity.Staff;
import com.three.mapper.BranchMapper;
import com.three.mapper.StaffMapper;
import com.three.service.BranchService;
import com.three.vo.AddBranchVo;
import com.three.vo.BranchVo;
import com.three.vo.DzDiaoLiVo;
import com.three.vo.R;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private HttpSession session;

    @Override
    public R getBranchName() {
        //判断登陆进来的账号是否为最大权限
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getBranchid().equals(12138)) {
            return R.ok(branchMapper.getAll());
        } else {
            return R.ok(branchMapper.getBranchByBranchId(staff.getBranchid()));
        }
    }

    //查询所有商铺信息和根据店长id查询店长名称
    @Override
    public R getAllAndName(Integer currentPage, Integer pageSize, BranchVo branchname) {
        //System.out.println(branchname+"@@@@@@@@@@@@@@@@@");
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        if (!branchid.equals(12138)){
            branchname.setBranchid(branchid);
        }
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        List<Branch> list = branchMapper.getAllAndName(branchname);
        //System.out.println(list);
        PageInfo<Branch> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    //添加商铺
    @Override
    @Transactional//事务
    public R addBranch(AddBranchVo addBranch) {
        //System.out.println(addBranch);

        //将不是店长的员工  所在店铺修改为新店铺
        int i = staffMapper.updateByBranchidRoleid
                (addBranch.getStaffid(), addBranch.getBranchid());
        //System.out.println
                //(addBranch.getStaffid() + "~~~~" + addBranch.getBranchid() + "~~~~222222222222修改信息前打印" + i);
        if (i > 0) {
            //添加商铺信息
            int i1 = branchMapper.addBranch(addBranch);
            if (i1 > 0) {
                return R.ok();
            } else {
                return R.error("添加失败");
            }
        } else {
            return R.error("添加失败");
        }
    }

    ////判断商铺编号是否重复
    @Override
    public R checkBranchid(Integer branchid) {
        Branch branch = branchMapper.checkBranchid(branchid);
        if (branch != null) {
            return R.error("该编号已存在");
        } else {
            return R.ok();
        }
    }

    //判断商铺名称是否重复
    @Override
    public R checkBranchname(String branchname) {
        Branch branch = branchMapper.checkBranchname(branchname);
        if (branch != null) {
            return R.error("该编号已存在");
        } else {
            return R.ok();
        }
    }

    //根据id查询商铺信息
    @Override
    public R getBranchByBranchId(Integer branchid) {
        Branch branch = branchMapper.getByBranchId(branchid);
        //System.out.println(branch);
        return R.ok(branch);
    }

    //修改商铺信息
    @Override
    public R updateBranch(Branch branch) {
        //System.out.println(branch);
        int i = branchMapper.updateByPrimaryKey(branch);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("修改失败");
        }
    }

    //修改商铺状态
    @Override
    public R updateState(Integer branchid, Integer state) {
        //如果是1营业状态，就改为0，整顿
        if (state == 1) {
            state = 0;
            branchMapper.updateState(branchid, state);
            return R.ok();
            //如果是0整顿状态，就改为1，营业
        } else if (state == 0) {
            state = 1;
            branchMapper.updateState(branchid, state);
            return R.ok();
        } else {
            return R.error("修改失败");
        }
    }

    //删除业务，先查询店铺人数是否还有人，有则不能删除
    @Override
    public R deleteBranch(Integer branchid) {
        int a=staffMapper.selectByBranchidCount(branchid);
        //System.out.println(a+"~~~~~~");
        //如果是0，则可以删除
        //如果是1，则不能删除，该部门还有员工
        if (a==0){
            int i = branchMapper.deleteBybranchid(branchid);
            if (i>0){
                return R.ok();
            }else {
                return R.error("删除失败");
            }
        }else {
            return R.error("该部门还有员工，无法删除");
        }
    }

    //修改店长，将该员工修改为店长，该店铺不变
    @Override
    public R updateDianZhang(Integer branchid, Integer staffid) {
        //将该员工信息改为当前店铺店长，角色为店长
        int i = staffMapper.updateDianZhang(branchid, staffid);
        if (i > 0){
            //修改成功
            int i1 = branchMapper.updateDianZhang(branchid, staffid);
            if (i1 > 0){
                return R.ok();
            }else {
                return R.error("修改失败");
            }
        }else {
            return R.error("修改失败");
        }
    }
    //查询所有门店不包含小张店
    @Override
    public R selectAll() {
        List<Branch> all = branchMapper.getAll();
        return R.ok(all);
    }

    //店长调离，更换所在部门，更换角色id。职位
    @Override
    public R dianZhangDiaoLi(DzDiaoLiVo dzDiaoLiVo) {
        //1.更换当前部门的店长
        int i = branchMapper.updateByBraanchname(dzDiaoLiVo.getBranchname());
        //System.out.println(dzDiaoLiVo+"~~~~~~~");
        if (i>0){
            //2.更换角色的身份，以及所在的部门，和职位
            int i1 = staffMapper.updateBranchRole(dzDiaoLiVo);
            if (i1>0){
                return R.ok();
            }else {
                return R.error("操作失败");
            }
        }else {
            return R.error("操作失败");
        }
    }

    //得到当前登陆人的店铺信息
    @Override
    public R getNewBranch() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        String branchname=branchMapper.getByBranchId(branchid).getBranchname();
        //System.out.println(branchid+"123"+branchname);
        return R.ok(branchname);
    }

    @Override
    public R getBranchName02() {
        List<Branch> all = branchMapper.getAll();
        return R.ok(all);
    }

    @Override
    public R getUserBranchId() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        return R.ok(branchid);
    }

    @Override
    public R getUserBranchName() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        Branch userBranchName = branchMapper.getUserBranchName(branchid);
        return R.ok(userBranchName.getBranchname());
    }


}
